package com.example.mvvm.base.vm;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ViewModel;

import com.example.mvvm.base.annotation.Dialog;
import com.example.mvvm.base.data.MutableLiveListData;
import com.example.mvvm.base.vm.action.FrameWorkModel;
import com.example.mvvm.base.vm.action.UIChangeObservable;
import com.example.mvvm.base.vm.action.ViewModelLife;
import com.example.mvvm.base.vm.action.ViewModelRx;

import java.lang.reflect.Method;
import java.util.ArrayList;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import sion.my.netmanger2.enums.NetMode;

/**
 * #FrameWorkModel#这里主要处理与activity之间的交互
 */
public abstract class BaseViewModel extends ViewModel implements ViewModelLife, ViewModelRx, Consumer<Disposable> {
    public MutableLiveListData<String> testData=new MutableLiveListData();
    //管理activity事件传递
    private FrameWorkModel frameWorkModel;
    //管理RxJava，主要针对RxJava异步操作造成的内存泄漏
    private CompositeDisposable mCompositeDisposable;
    //管理Ui事件传递
    private UIChangeObservable ui;

    public FrameWorkModel getContext() {
        if (frameWorkModel == null) {
            frameWorkModel = new FrameWorkModel();
        }
        return frameWorkModel;
    }

    public UIChangeObservable getUi() {
        if (ui == null) {
            ui = new UIChangeObservable();
        }
        return ui;
    }

    @Override
    public void netWorkChange(NetMode netWorkEvent) {

    }

    /**
     * 添加事件
     */
    @Override
    public void addSubscribe(Disposable disposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.add(disposable);
    }

    /**
     * 销毁事件
     */
    @Override
    public void removeEvent() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
        }
    }

    /**
     * 请大家在这里注册事件
     */
    @Override
    public void registerRxBus() {
    }

    ;

    /**
     * 初始化
     */
    public void initViewModel() {
        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            strings.add("");
        }
        testData.setValue(strings);
        registerRxBus();
    }

    ;

    /**
     * 重启
     */
    public void onResume() {
    }

    ;

    /**
     * 销毁
     */
    @Override
    public void onDestroy() {
        removeEvent();
    }

    ;

    /**
     * 暂停
     */
    public void onPause() {
    }

    ;


    /**
     * 开始
     */
    public void onStart() {
    }

    ;

    /**
     * 停止
     */
    public void onStop() {

    }

    @Override
    public void accept(Disposable disposable) throws Exception {
        addSubscribe(disposable);
        //获取堆栈跟踪
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace != null) {
            stack:
            for (StackTraceElement stackTraceElement : stackTrace) {
                if (stackTraceElement.getClassName() != null) {
                    //如果是model模块的就要判断是否弹窗
                    if (stackTraceElement.getClassName().contains("com.sx.baseproject.framework.model")) {
                        String methodName = stackTraceElement.getMethodName();
                        Class<?> modelClass = Class.forName(stackTraceElement.getClassName());
                        for (Method declaredMethod : modelClass.getDeclaredMethods()) {
                            if (declaredMethod.getName().equals(methodName)) {
                                Dialog annotation = declaredMethod.getAnnotation(Dialog.class);
                                if (annotation != null) {
                                    getUi().showDialog(annotation.tip(),annotation.touch());
                                    break stack;
                                }
                            }

                        }

                    }
                }

            }
        }
    }

}
